//
//  PasswordMaintenanceNew.m
//  cocoapodText
//
//  Created by apple on 16/10/18.
//  Copyright © 2016年 apple. All rights reserved.
//


#import "ShiPei.h"
#import <Foundation/Foundation.h>
#import "PasswordMaintenanceNew.h"
@interface PasswordMaintenanceNew ()

@end

@implementation PasswordMaintenanceNew

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view setBackgroundColor:FUIColorFromRGB(0xf4f4f4)];
    [self GetMainContentView];
}

/**
 * 边界
 * top:(0, 0, kScreenW, 10)
 * bottom:(0, kScreenH-10, kScreenW,10)
 * left:(0, 0, 10, kScreenH)
 * right:(kScreenW-170, 0, 10, kScreenH)
 */
-(void)GetMainContentView{
    UIView *allBackView=[[UIView alloc]initWithFrame:CGRectMake([ShiPei CGFloatMakeScale:15], [ShiPei CGFloatMakeScale:15], [ShiPei CGFloatMakeScale:TabChildContentW], kScreenH-CommonBarH-kStatusH-[ShiPei CGFloatMakeScale:30]-[ShiPei CGFloatMakeScale:FSCommonBottomH])];
    [self.view addSubview:allBackView];
    [allBackView setBackgroundColor:[UIColor whiteColor]];
    [allBackView setUserInteractionEnabled:YES];
    
    //内容栏
    _passAllBackView=[[UIView alloc]initWithFrame:CGRectMake(0, 0, [ShiPei CGFloatMakeScale:TabChildContentW], kScreenH-CommonBarH-kStatusH-[ShiPei CGFloatMakeScale:30]-[ShiPei CGFloatMakeScale:FSCommonBottomH])];
    [allBackView addSubview:_passAllBackView];
    [_passAllBackView setBackgroundColor:[UIColor clearColor]];
    [_passAllBackView setUserInteractionEnabled:YES];
    /*************************************/
    _userInfoViewBack=[[UIView alloc]initWithFrame:[ShiPei CGRectMakeScale:0 :100 :TabChildContentW :220]];
    [_passAllBackView addSubview:_userInfoViewBack];
    [_userInfoViewBack setBackgroundColor:[UIColor clearColor]];
    //头像
    UIImageView *TitleUserInfoImageView=[[UIImageView alloc]initWithFrame:[ShiPei CGRectMakeScale:TabChildContentW/2-30 :0 :60 :60]];
    [TitleUserInfoImageView.layer setMasksToBounds:YES];
    [TitleUserInfoImageView.layer setCornerRadius:[ShiPei CGFloatMakeScale:17.5]];
    [TitleUserInfoImageView setImage:[UIImage imageNamed:@"user_160*160.jpg"]];
    [_userInfoViewBack addSubview:TitleUserInfoImageView];
    
    NSString *userName=[CommonTool getValueFromKey:@"username" defaultValue:@""];
    //用户名
    UILabel *userNameLabel1=[[UILabel alloc]initWithFrame:[ShiPei CGRectMakeScale:TabChildContentW/2-250 :65 :500 :45]];
    [_userInfoViewBack addSubview:userNameLabel1];
    [userNameLabel1 setText:[@"用户名:" stringByAppendingString:userName]];
    [userNameLabel1 setTextColor:FUIColorFromRGB(0x333333)];
    [userNameLabel1 setTextAlignment:NSTextAlignmentCenter];
    [userNameLabel1 setFont:[UIFont boldSystemFontOfSize:[ShiPei CGFloatMakeFontScaleWithPX:30]]];
    /*************************************/
    //用户密码
    UIView *userPassBackView=[[UIView alloc]initWithFrame:[ShiPei CGRectMakeScale:TabChildContentW/2-250 :220 :500 :45]];
    [_passAllBackView addSubview:userPassBackView];
    [userPassBackView setUserInteractionEnabled:YES];
    [userPassBackView.layer setBorderColor:[FUIColorFromRGB(0xcfdbe3) CGColor]];
    [userPassBackView.layer setBorderWidth:1];
    //将图层的边框设置为圆脚
    [userPassBackView.layer setCornerRadius:8];
    [userPassBackView.layer setMasksToBounds:YES];
    
    UIImageView *userPassImage=[[UIImageView alloc]initWithFrame:[ShiPei CGRectMakeScale:5 :10 :25 :25]];
    [userPassBackView addSubview:userPassImage];
    [userPassImage setImage:[UIImage imageNamed:@"fs_login_pass.png"]];
    
    _userPassEdit=[[UITextField alloc]initWithFrame:[ShiPei CGRectMakeScale:35 :0 :450 :45]];
    [userPassBackView addSubview:_userPassEdit];
    _userPassEdit.clearButtonMode = UITextFieldViewModeWhileEditing; //编辑时会出现个修改X
    [_userPassEdit setBackgroundColor:[UIColor whiteColor]];
    [_userPassEdit setPlaceholder:@"请输入旧密码"];
    [_userPassEdit setText:@""];
    [_userPassEdit setDelegate:self];
    [_userPassEdit setTextColor:FUIColorFromRGB(0x333333)];
    _userPassEdit.secureTextEntry = YES;
    [_userPassEdit setFont:[UIFont boldSystemFontOfSize:[ShiPei CGFloatMakeFontScaleWithPX:30]]];
    
    //新密码
    UIView *userNewPassBackView=[[UIView alloc]initWithFrame:[ShiPei CGRectMakeScale:TabChildContentW/2-250 :290 :500 :45]];
    [_passAllBackView addSubview:userNewPassBackView];
    [userNewPassBackView setUserInteractionEnabled:YES];
    [userNewPassBackView.layer setBorderColor:[FUIColorFromRGB(0xcfdbe3) CGColor]];
    [userNewPassBackView.layer setBorderWidth:1];
    //将图层的边框设置为圆脚
    [userNewPassBackView.layer setCornerRadius:8];
    [userNewPassBackView.layer setMasksToBounds:YES];
    
    UIImageView *userNewPassImage=[[UIImageView alloc]initWithFrame:[ShiPei CGRectMakeScale:5 :10 :25 :25]];
    [userNewPassBackView addSubview:userNewPassImage];
    [userNewPassImage setImage:[UIImage imageNamed:@"fs_login_pass.png"]];
    
    _userNewPassEdit=[[UITextField alloc]initWithFrame:[ShiPei CGRectMakeScale:35 :0 :450 :45]];
    [userNewPassBackView addSubview:_userNewPassEdit];
    _userNewPassEdit.clearButtonMode = UITextFieldViewModeWhileEditing; //编辑时会出现个修改X
    [_userNewPassEdit setBackgroundColor:[UIColor whiteColor]];
    [_userNewPassEdit setPlaceholder:@"新密码,长度为4-16位"];
    [_userNewPassEdit setText:@""];
    [_userNewPassEdit setDelegate:self];
    _userNewPassEdit.secureTextEntry = YES;
    [_userNewPassEdit setTextColor:FUIColorFromRGB(0x333333)];
    [_userNewPassEdit setFont:[UIFont boldSystemFontOfSize:[ShiPei CGFloatMakeFontScaleWithPX:30]]];
    
    //确认新密码
    UIView *userConfrimNewPassBackView=[[UIView alloc]initWithFrame:[ShiPei CGRectMakeScale:TabChildContentW/2-250 :360 :500 :45]];
    [_passAllBackView addSubview:userConfrimNewPassBackView];
    [userConfrimNewPassBackView setUserInteractionEnabled:YES];
    [userConfrimNewPassBackView.layer setBorderColor:[FUIColorFromRGB(0xcfdbe3) CGColor]];
    [userConfrimNewPassBackView.layer setBorderWidth:1];
    //将图层的边框设置为圆脚
    [userConfrimNewPassBackView.layer setCornerRadius:8];
    [userConfrimNewPassBackView.layer setMasksToBounds:YES];
    
    UIImageView *userConfrimNewPassImage=[[UIImageView alloc]initWithFrame:[ShiPei CGRectMakeScale:5 :10 :25 :25]];
    [userConfrimNewPassBackView addSubview:userConfrimNewPassImage];
    [userConfrimNewPassImage setImage:[UIImage imageNamed:@"fs_login_pass.png"]];
    
    _userConfrimNewPassEdit=[[UITextField alloc]initWithFrame:[ShiPei CGRectMakeScale:35 :0 :450 :45]];
    [userConfrimNewPassBackView addSubview:_userConfrimNewPassEdit];
    _userConfrimNewPassEdit.clearButtonMode = UITextFieldViewModeWhileEditing; //编辑时会出现个修改X
    [_userConfrimNewPassEdit setBackgroundColor:[UIColor whiteColor]];
    [_userConfrimNewPassEdit setPlaceholder:@"请重复输入新密码"];
    [_userConfrimNewPassEdit setText:@""];
    [_userConfrimNewPassEdit setDelegate:self];
    _userConfrimNewPassEdit.secureTextEntry = YES;
    [_userConfrimNewPassEdit setTextColor:FUIColorFromRGB(0x333333)];
    [_userConfrimNewPassEdit setFont:[UIFont boldSystemFontOfSize:[ShiPei CGFloatMakeFontScaleWithPX:30]]];
    
    //在弹出的键盘上面加一个view来放置退出键盘的Done按钮
    UIToolbar * topView = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 30)];
    [topView setBarStyle:UIBarStyleDefault];
    UIBarButtonItem * btnSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
    UIBarButtonItem * doneButton = [[UIBarButtonItem alloc] initWithTitle:@"完成" style:UIBarButtonItemStyleDone target:self action:@selector(dismissKeyBoard)];
    NSArray * buttonsArray = [NSArray arrayWithObjects:btnSpace, doneButton, nil];
    [topView setItems:buttonsArray];
    [_userPassEdit setInputAccessoryView:topView];
    [_userNewPassEdit setInputAccessoryView:topView];
    [_userConfrimNewPassEdit setInputAccessoryView:topView];
    
    //提交按钮
    UIButton *submitChangePassButton=[[UIButton alloc]initWithFrame:[ShiPei CGRectMakeScale:TabChildContentW/2-250 :430 :500 :45]];
    [_passAllBackView addSubview:submitChangePassButton];
    [submitChangePassButton setBackgroundImage:[CommonTool createImageWithColor:FUIColorFromRGB(0x3388cb) Frame:[ShiPei CGRectMakeScale:0 :0 :50 :25]] forState:UIControlStateNormal];
    [submitChangePassButton setBackgroundImage:[CommonTool createImageWithColor:FUIColorFromRGB(0x639ed7)  Frame:[ShiPei CGRectMakeScale:0 :0 :50 :25]] forState:UIControlStateHighlighted];
    [submitChangePassButton setUserInteractionEnabled:YES];
    [submitChangePassButton setTitle:@"保存" forState:UIControlStateNormal];
    [submitChangePassButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [submitChangePassButton.titleLabel setFont:[UIFont boldSystemFontOfSize:[ShiPei CGFloatMakeFontScaleWithPX:40]]];
    [submitChangePassButton.layer setMasksToBounds:YES];
    [submitChangePassButton.layer setCornerRadius:5];
    [submitChangePassButton addTarget:self action:@selector(onSaveNewPass:) forControlEvents:UIControlEventTouchUpInside];
    
}

-(void)onSaveNewPass:(id)sender{
    //先检查非空
    _oldPassWordStr=_userPassEdit.text;
    _changePassWordStr=_userNewPassEdit.text;
    NSString *newConfrimPass=_userConfrimNewPassEdit.text;
    
    if ([_oldPassWordStr isEqualToString:@""]) {
        [CommonTool showAllTextDialogWithText:@"旧密码为空" parentView:_passAllBackView];
    }else{
        if([_oldPassWordStr length]<6){
            [CommonTool showAllTextDialogWithText:@"旧密码至少4位" parentView:_passAllBackView];
        }else{
            if ([_changePassWordStr isEqualToString:@""]) {
                [CommonTool showAllTextDialogWithText:@"新密码为空" parentView:_passAllBackView];
            }else{
                if ([_changePassWordStr length]<6) {
                    [CommonTool showAllTextDialogWithText:@"新密码长度必须至少4位" parentView:_passAllBackView];
                }else{
                    if ([newConfrimPass isEqualToString:@""]) {
                        [CommonTool showAllTextDialogWithText:@"确认密码为空" parentView:_passAllBackView];
                    }else{
                        //首先对比新旧密码
                        if (![_changePassWordStr isEqualToString:newConfrimPass]) {
                            [CommonTool showAllTextDialogWithText:@"新密码与确认密码不同" parentView:_passAllBackView];
                        }else{
                            APIAccountManager *manager=[APIAccountManager alloc];
                            [manager setDelegate:self];
                            NSString *userName=[CommonTool getValueFromKey:@"username" defaultValue:@""];
                            [manager fsgy_api_info_account_pass_change_withName:userName UserOldPassword:_oldPassWordStr UserPassword:_changePassWordStr];
                        }
                    }
                }
            }
        }
    }
}

-(void)onAccountManager:(APIAccountManager *)manager result:(NSDictionary *)result{
    //{"flag":"1","reason":""}
    //flag：成功失败标识 1：成功  2：失败
    NSString *flag=[CommonTool getStringFromNSDictionary:result key:@"code" defaultValue:@"2"];
    NSString *message=@"";
    if ([flag isEqualToString:@"1"]) {
        message=@"密码修改成功";
    }else{
        message=@"密码修改失败，请检查密码是否填写正确";
    }
    [CommonTool showAllTextDialogWithText:message parentView:_passAllBackView];
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
}

-(void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    //重新加载置空
    [self.userPassEdit setText:@""];
    [self.userNewPassEdit setText:@""];
    [self.userConfrimNewPassEdit setText:@""];
}

-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
}

//关闭键盘
-(void) dismissKeyBoard{
    [_userPassEdit resignFirstResponder];
    [_userNewPassEdit resignFirstResponder];
    [_userConfrimNewPassEdit resignFirstResponder];
}

//开始编辑输入框的时候，软键盘出现，执行此事件
-(void)textFieldDidBeginEditing:(UITextField *)textField{
    NSTimeInterval animationDuration = 0.30f;
    [UIView beginAnimations:@"ResizeForKeyboard" context:nil];
    [UIView setAnimationDuration:animationDuration];
    //将视图的Y坐标向上移动offset个单位，以使下面腾出地方用于软键盘的显示
    //self.view.frame = CGRectMake(0.0f, -240, self.view.frame.size.width, self.view.frame.size.height);
    _passAllBackView.frame=CGRectMake([ShiPei CGFloatMakeScale:15], [ShiPei CGFloatMakeScale:15]-210, [ShiPei CGFloatMakeScale:TabChildContentW], kScreenH-CommonBarH-kStatusH-[ShiPei CGFloatMakeScale:30]-[ShiPei CGFloatMakeScale:FSCommonBottomH]);
    
    [UIView commitAnimations];
    [_userInfoViewBack setHidden:YES];
}

//当用户按下return键或者按回车键，keyboard消失
-(BOOL)textFieldShouldReturn:(UITextField *)textField{
    [textField resignFirstResponder];
    return YES;
}
//当用户按下return键
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string  {
    if ([string isEqualToString:@"\n"]) {
        [textField resignFirstResponder];
        return NO;
    }
    //限制输入长度
    //这里的if时候为了获取删除操作,如果没有次if会造成当达到字数限制后删除键也不能使用的后果.
    if (range.length == 1 && string.length == 0) {
        return YES;
    }else if (textField.text.length >= 16) {
        textField.text = [textField.text substringToIndex:16];
        [CommonTool showAllTextDialogWithText:@"最大长度16位" parentView:_passAllBackView];
        return NO;
    }
    return YES;
}

//输入框编辑完成以后，将视图恢复到原始状态
-(void)textFieldDidEndEditing:(UITextField *)textField{
    //self.view.frame =CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
    _passAllBackView.frame=CGRectMake([ShiPei CGFloatMakeScale:15], [ShiPei CGFloatMakeScale:15], [ShiPei CGFloatMakeScale:TabChildContentW], kScreenH-CommonBarH-kStatusH-[ShiPei CGFloatMakeScale:30]-[ShiPei CGFloatMakeScale:FSCommonBottomH]);
    [_userInfoViewBack setHidden:NO];
}


@end
